package tm.testing;

import javax.swing.ImageIcon;

import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.input.MouseInput;
import com.jme.input.MouseInputListener;
import com.jme.intersection.TrianglePickResults;
import com.jme.math.Ray;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jmex.terrain.TerrainPage;
import com.jmex.terrain.util.ImageBasedHeightMap;

public class PickTestNumber6 extends SimpleGame implements MouseInputListener {
	
	private Node terrainNode;
	private Node geom;
	
	@Override
	protected void simpleInitGame() {
		terrainNode = new Node();
		geom = new Node();
		Box box = new Box("TESTBOX1", new Vector3f(0,0,0), 10, 10, 10);
		box.setModelBound(new BoundingBox());
		box.updateModelBound();
		geom.attachChild(box);
		rootNode.attachChild(geom);
		
		ImageIcon image = new ImageIcon(PickTestNumber6.class.getClassLoader().getResource("data/textures/testterrain.png"));
		  
		  
		ImageBasedHeightMap heightMap = new ImageBasedHeightMap(image.getImage());
		Vector3f terrainScale = new Vector3f(10,1,10);
		heightMap.setHeightScale( 0.001f);
		TerrainPage page = new TerrainPage("Terrain", 33, heightMap.getSize(), terrainScale,
				  					heightMap.getHeightMap());
		
		page.setDetailTexture(1, 64);
		terrainNode.attachChild(page);
		    
		Texture testgrass = TextureManager.loadTexture(TestOne.class.getClassLoader().
		          getResource(
		                  "data/textures/nicegrass.jpg"),
		                                                      Texture.MinificationFilter.Trilinear,
		                                                      Texture.MagnificationFilter.Bilinear);
		testgrass.setScale(new Vector3f(90.0f,90.0f,1.0f));
		    
		TextureState ts = display.getRenderer().createTextureState();
		ts.setEnabled(true);
		
		    
		ts.setTexture(testgrass, 0);
		testgrass.setWrap(Texture.WrapMode.Repeat);
		 
		
		Texture t2 = TextureManager.loadTexture(TestOne.class.getClassLoader().
		                                            getResource(
		      "data/textures/Untitled.png"),
		                                          Texture.MinificationFilter.Trilinear,
		                                          Texture.MagnificationFilter.Bilinear);
		ts.setTexture(t2, 1);
		t2.setWrap(Texture.WrapMode.Repeat);
		
		
		testgrass.setApply(Texture.ApplyMode.Combine);
		testgrass.setCombineFuncRGB(Texture.CombinerFunctionRGB.Modulate);
		testgrass.setCombineSrc0RGB(Texture.CombinerSource.CurrentTexture);
		testgrass.setCombineOp0RGB(Texture.CombinerOperandRGB.SourceColor);
		testgrass.setCombineSrc1RGB(Texture.CombinerSource.PrimaryColor);
		testgrass.setCombineOp1RGB(Texture.CombinerOperandRGB.SourceColor);
		  
		
		t2.setApply(Texture.ApplyMode.Combine);
		t2.setCombineFuncRGB(Texture.CombinerFunctionRGB.AddSigned);
		t2.setCombineSrc0RGB(Texture.CombinerSource.CurrentTexture);
		t2.setCombineOp0RGB(Texture.CombinerOperandRGB.SourceColor);
		t2.setCombineSrc1RGB(Texture.CombinerSource.Previous);
		t2.setCombineOp1RGB(Texture.CombinerOperandRGB.SourceColor);
		terrainNode.setRenderState(ts);
		rootNode.attachChild(terrainNode);
		
		
		
		MouseInput.get().addListener(this);
		MouseInput.get().setCursorVisible(true);

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	    PickTestNumber6 app = new PickTestNumber6();
	    app.setConfigShowMode(ConfigShowMode.AlwaysShow);
	    app.start();
	}

	
	private void processPick() {
		System.out.println("PICK!");
    		
    	//mouseState.setMousePosition(new Coordinate(mouse_x, mouse_y));
    		
    	Vector2f screenPos = new Vector2f();
    	//Get the position that the mouse is pointing to
    	
		screenPos.set(MouseInput.get().getXAbsolute(), MouseInput.get().getYAbsolute());
		System.out.println(screenPos.x + "AHA "+ screenPos.y);
		// Get the world location of that X,Y value
		Vector3f worldCoords = display.getWorldCoordinates(screenPos, 1.0f);
		// Create a ray starting from the camera, and going in the direction
        // of the mouse's location
        final Ray mouseRay = new Ray(cam.getLocation(), worldCoords
                .subtractLocal(cam.getLocation()));
        mouseRay.getDirection().normalizeLocal();
        TrianglePickResults results = new TrianglePickResults();
        results.clear();
        results.setCheckDistance(true);
        geom.findPick(mouseRay,results);
        
    
    	if(results.getNumber() > 0) {
    		System.out.println("VORHER: "+results.getNumber()+" AHA: "+results.getPickData(0).getTargetMesh().getName());
    	}
    	
	}

	public void onButton(int button, boolean pressed, int x, int y) {
		if(button==0 && pressed ==false) {
			processPick();
		}
		
	}

	public void onMove(int xDelta, int yDelta, int newX, int newY) {
		// TODO Auto-generated method stub
		
	}

	public void onWheel(int wheelDelta, int x, int y) {
		// TODO Auto-generated method stub
		
	}

}
